package com.hevs.samplewebapp.server.json;

import java.io.IOException;

import org.codehaus.jackson.JsonGenerator;
import org.codehaus.jackson.JsonProcessingException;
import org.codehaus.jackson.map.JsonSerializer;
import org.codehaus.jackson.map.SerializerProvider;

import com.google.appengine.api.datastore.Key;
import com.google.appengine.api.datastore.KeyFactory;

/**
 * Object used by Jackson to deserialize JSON key string to
 * com.google.appengine.api.datastore.Key
 * 
 * @author Arnaud Durand
 * @date 29.03.2012
 */
public class KeySerializer extends JsonSerializer<Key>{

	/**
	 * Method to serialize the Json
	 * @param value
	 * Java object to serialize
	 * @param jgen
	 * JSON generator
	 * @throws IOException, JsonProcessingException
	 * exception when the JSON isn't correct.
	 */
	@Override
	public void serialize(Key value, JsonGenerator jgen,
			SerializerProvider provider) throws IOException,
			JsonProcessingException {
		
		jgen.writeString(KeyFactory.keyToString(value));
		
	}

}
